package org.csu.mypetstore.service.impl;

import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.persistence.ItemMapper;
import org.csu.mypetstore.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    ItemMapper itemMapper;

    @Override
    public List<Category> findAllCategory() {
        return itemMapper.selectAllCategory();
    }

    @Override
    public List<Category> findCategoryByName(String keyword) {
        return itemMapper.selectCategoryByName("%" + keyword + "%");
    }

    @Override
    public void insertCategory(String categoryId, String name, String description) {
        itemMapper.insertCategory(categoryId, name, description);
    }

    @Override
    public void deleteCategoryByCategoryId(String categoryId) {
        itemMapper.deleteCategoryByCategoryId(categoryId);
    }

    @Override
    public void updateCategoryByCategoryId(String categoryId, String name, String description) {
//        itemMapper.updateCategoryByCategoryId(updateCategoryId,categoryId,name,description);
        itemMapper.updateCategoryByCategoryId(categoryId,name,description);
    }

    @Override
    public List<Product> findAllProduct() {
        return itemMapper.selectAllProduct();
    }

    @Override
    public List<Product> findProductByName(String keyword) {
        return itemMapper.selectProductByName("%" + keyword + "%");
    }

    @Override
    public void insertProduct(String productId, String category, String name, String description) {
        itemMapper.insertProduct(productId, category, name, description);
    }

    @Override
    public void deleteProductByProductId(String productId) {
        itemMapper.deleteProductByProductId(productId);
    }

//    @Override
//    public void updateProductByProductId(String updateProductId, String productId, String category, String name, String description) {
//        itemMapper.updateProductByProductId(updateProductId,productId,category,name,description);
//    }

    @Override
    public void updateProductByProductId(String productId, String name, String description) {
        itemMapper.updateProductByProductId(productId,name,description);
    }

    @Override
    public List<Item> findAllItem() {
        List<Item> list = itemMapper.selectAllItem();
        for (int i = 0; i < list.size(); i++) {
            int inventory = itemMapper.selectInventoryByItemId(list.get(i).getItemId());
            list.get(i).setQuantity(inventory);
        }
        return list;
    }

    @Override
    public List<Item> findItemByItemId(String itemId) {
        List<Item> list = itemMapper.selectItemByItemId("%" + itemId + "%");
        for (int i = 0; i < list.size(); i++) {
            int inventory = itemMapper.selectInventoryByItemId(list.get(i).getItemId());
            list.get(i).setQuantity(inventory);
        }
        return list;
    }

    @Override
    public List<Item> findItemByProductId(String productId) {
        List<Item> list = itemMapper.selectItemByProductId("%" + productId + "%");
        for (int i = 0; i < list.size(); i++) {
            int inventory = itemMapper.selectInventoryByItemId(list.get(i).getItemId());
            list.get(i).setQuantity(inventory);
        }
        return list;
    }

    @Override
    public List<Item> findItemByItemIdAndProductId(String itemId, String productId) {
        List<Item> list = itemMapper.selectItemByItemIdAndProductId("%" + itemId + "%","%" + productId + "%");
        for (int i = 0; i < list.size(); i++) {
            int inventory = itemMapper.selectInventoryByItemId(list.get(i).getItemId());
            list.get(i).setQuantity(inventory);
        }
        return list;
    }

    @Override
    public void insertItem(Item item) {
        itemMapper.insertItem(item);
        itemMapper.insertItemInventory(item);
    }

    @Override
    public void deleteItemByItemId(String itemId) {
        itemMapper.deleteItemByItemId(itemId);
        itemMapper.deleteInventoryItemId(itemId);
    }

//    @Override
//    public void updateItemByItemId(String updateItemId, Item item) {
//        itemMapper.updateItemByItemId(updateItemId,item);
//        itemMapper.updateInventoryByItemId(updateItemId,item.getItemId(),item.getQuantity());
//    }
@Override
    public void updateItemByItemId(Item item) {
        itemMapper.updateItemByItemId(item);
        itemMapper.updateInventoryByItemId(item.getItemId(),item.getQuantity());
    }
}
